package au.edu.deakin.ice.meltdown;

import java.util.HashMap;

import android.annotation.SuppressLint;
import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;


/** Responsible for managing a SoundPool and exposing the sound playing capabilities to the parent app */
@SuppressLint("UseSparseArrays")
public class SoundManager {
	
/*
 * use
 * SOUNDPOOOL
 * example http://blog.nelsondev.net/?p=207
 * 
 * 
 */
	/** SoundPool */
	private SoundPool mSounds;
	/** A map of valid sound IDs, generated by SoundManager to resource ID's that can be used to play sounds */
	private HashMap<Integer, Integer> soundsMap;
	/** Number of sounds loaded */
	private int soundCount;
	/** App context used to laad sounds */
	private static Context context;
	/** whether to play any sounds or not */
	private static boolean enabled = true;
	
	/** Pass app context so that soundmanager can load sounds from it
	 * @param c The context to store */
	public static void setContext(Context c){
		context = c;
	}
	
	/** enable all sound managers, they will  play sounds normally*/
	public static void enable(){
		enabled = true;
	}
	
	/** disable all sound managers, they will now refuse to start playing any sounds*/
	public static void disable(){
		enabled = false;
	}
	
	/** test if the sounds are enabled
	 * 
	 * @return enabled
	 */
	public static boolean isEnabled(){
		return enabled;
	}
	
	/** Constructor */
	@SuppressLint("UseSparseArrays") //we need hash maps so that we can search and shut down any dangling sounds in OnDestroy
	public SoundManager() {
		mSounds = new SoundPool(4, AudioManager.STREAM_MUSIC , 0);
		soundsMap = new HashMap<Integer, Integer>();
		soundCount = 0;
	}
	
	/** Load a sounds into memory
	 * @param soundResource The resource id to load eg. R.res.soundfile
	 * @return the sound id that can be used to play the sound */
	public int load(int soundResource){
		if(mSounds == null) {
			return -1;
		}
		
		soundsMap.put(++soundCount, mSounds.load(context, soundResource, 1));
		
		return soundCount;
	}
	
	/** Play a sound 
	 * @param soundid the sound to play
	 * @param loop True to loop the sound false to play only once*/
	public void play(int soundid, boolean loop){
		if(!soundsMap.containsKey(soundid))
			return;
		
		int looping = 0;
		if(loop)
			looping = -1;
		
		if(soundid < 0 || soundid > soundCount)
			return; //sound wasn't loaded correctly or a junk id is being passed.
			
		//only play sound if sounds are enabled.
		//if the play fails, then unload the sound
		//and remove it from the sound map
		if(enabled)
			if(mSounds.play(soundsMap.get(soundid), 1, 1, 1, looping, 1) == 0){
				//mSounds.unload(soundid);
				soundsMap.remove(soundid);
			}
				
	}
	
	/** Pause a sound 
	 * @param sound the sound to pause*/
	public void pause(int sound){
		if(soundsMap.containsKey(sound))
			mSounds.pause(sound);
	}
	/** Resume a sound 
	 * @param sound the sound to resume*/
	public void resume(int sound){
		if(enabled && soundsMap.containsKey(sound))
			mSounds.resume(sound);
	}
	/** stop playing a sound 
	 * @param sound the sound to stop*/
	public void stop(int sound){
		mSounds.stop(sound);
	}
	
	/**stop playing any sounds and unload all sounds objects*/
	public void clear(){
		for (int value :  soundsMap.values()) {
		    mSounds.stop(value);
		    mSounds.unload(value);
		}
		
		soundsMap.clear();
	}
	/** Cleanup and stop any sounds */
	public void kill(){
		if(mSounds != null){
			clear();
			
			mSounds.release();
			mSounds = null;
		}
		
	}
}
